package com.lujieni.springbootflowable;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 候选人
 */
@SpringBootTest
class SpringbootFlowableApplicationTests3 {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     *
     * 流程部署
     * act_re_procdef 流程定义实例
     * act_re_deployment 流程定义部署
     *
     */
    @Test
    void deploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("process/example02.bpmn20.xml")
                .name("example02")
                .deploy();
        System.out.println(deployment.getId());
    }

    /**
     * 启动流程实例并设置全局参数
     * act_ru_variable表中会插入数据
     * act_ru_identitylink 表中会插入候选人数据
     */
    @Test
    void startProcessWithVariables() {
        Map<String, Object> variables = new HashMap<>();
        // 候选人姓名
        variables.put("c1", "c1");
        variables.put("c2", "c2");
        variables.put("c3", "c3");
        variables.put("c4", "c4");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("example02", variables);
    }

    /**
     * 根据候选人查询代办任务
     */
    @Test
    void findTaskByCandidate(){
        TaskService taskService = processEngine.getTaskService();
        // 根据候选人姓名查询它关联的任务
        List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("c1").list();
        for (Task task : tasks) {
            System.out.println(task.getId());
            // d7315c9e-8674-11f0-9af4-5847ca7eb09c
        }
    }

    /**
     * 候选人必须拾取任务才能完成
     * 目前ASSIGNEE_没有值,拾取之后才有处理人
     * 候选人 → 拾取(claim) → 审批人 → 完成任务
     */
    @Test
    public void claimTask(){
        taskService.claim("d7315c9e-8674-11f0-9af4-5847ca7eb09c", "c1");
    }

    /**
     * 取消任务拾取(将任务重新变为未分配状态)
     * 目前ASSIGNEE_有值,取消之后ASSIGNEE_变为null
     */
    @Test
    public void unclaimTask(){
        taskService.unclaim("d7315c9e-8674-11f0-9af4-5847ca7eb09c");
    }

    /**
     * 将任务指派给特定用户,本人不想处理了
     */
    @Test
    public void assigneeTask(){
        taskService.setAssignee("d7315c9e-8674-11f0-9af4-5847ca7eb09c", "jieni");
    }

    @Test
    public void completeTask(){
        TaskService taskService = processEngine.getTaskService();
        taskService.complete("d7315c9e-8674-11f0-9af4-5847ca7eb09c");
    }

}
